package com.itheima.tset;

import com.itheima.domian.Order;
import com.itheima.domian.User;
import com.itheima.dao.OrderMapper;
import com.itheima.dao.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Demo {
    private SqlSession sqlSession;

    @Before
    public void before() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        sqlSession = sqlSessionFactory.openSession();
    }

    @After
    public void after() {
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void test1() {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.findAll();
        for (User user : users) {
            System.out.println(user);
        }
    }

    @Test
    public void test2() {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.delete(4);
    }

    @Test
    public void test3() {
        User user = new User();
        user.setId(8);
        user.setUsername("黑户");
        user.setPassword("123");
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.update(user);
    }

    @Test
    public void test4() {
        User user = new User();
        //user.setId(4);
        user.setUsername("ddd");
        user.setPassword("123");
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.save(user);

    }

    @Test
    public void test6() {

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.findById(1);
        System.out.println(user);
    }

    @Test
    public void test5() {
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        List<Order> orders = mapper.findAll();
        for (Order order : orders) {
            System.out.println(order.getOrdertime());
        }
    }

    @Test
    public void test7() {
        User user = new User();
        user.setUsername("kk");
        user.setPassword("123");
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        System.out.println(user);
        mapper.save2(user);
        System.out.println(user);
    }
    @Test
    public void test8(){
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int count = mapper.count();
        System.out.println(count);
    }

}
